<template>
	<div class="wrap">
		<el-form ref="form" :inline="true" size="small" :model="forms" class="form" label-width="100px">
			<el-form-item label="广告标题" prop="focus_title">
				<el-input
					v-model="forms.focus_title"
					placeholder="请输入广告标题"
					clearable
					style="width: 200px"
				/>
			</el-form-item>
			<el-form-item label="发布时间" prop="cid_no">
				<el-date-picker
					v-model="forms.date"
					type="daterange"
					range-separator="至"
					start-placeholder="开始日期"
					end-placeholder="结束日期"
					value-format="yyyy-MM-dd"
				></el-date-picker>
			</el-form-item>
			<p class="line">
				<el-button icon="el-icon-search" type="primary" size="small" @click="loadData">
					搜索
				</el-button>
			</p>
		</el-form>
		<div class="answer-bot">
			<el-row style="margin-bottom: 15px">
				<div class="button">
					<el-button type="primary" size="small" plain @click="openAnswer('新增')">
						新增广告
					</el-button>
				</div>
			</el-row>
			<el-table
				stripe
				highlight-current-row
				:data="tableData"
				:resizable="false"
				:cell-style="() => ({ textAlign: 'center' })"
				:header-cell-style="() => ({ textAlign: 'center' })"
				size="small"
				border
			>
				<el-table-column label="序号" width="60px" type="index" />
				<el-table-column
					label="广告标题"
					min-width="150px"
					show-overflow-tooltip
					:formatter="row => row.focus_title || '--'"
				/>
				<el-table-column
					label="类型"
					min-width="150px"
					show-overflow-tooltip
					:formatter="row => (row.is_ad == 1 ? '首页弹窗' : row.is_ad == 0 ? '首页轮播' : '--')"
				/>
				<el-table-column
					label="跳转方式"
					min-width="150px"
					show-overflow-tooltip
					:formatter="
						row =>
							row.focus_type == 1
								? '自定图文'
								: row.focus_type == 2
								? '公众号链接'
								: row.focus_type == 3
								? '赛事详情'
								: '--'
					"
				/>
				<el-table-column
					label="发布时间"
					min-width="150px"
					show-overflow-tooltip
					:formatter="row => row.add_time || '--'"
				/>
				<el-table-column label="展示时间" min-width="230px" show-overflow-tooltip>
					<template slot-scope="{ row }">
						<span v-if="row.finish_time != 0">{{ row.begin_time }}~{{ row.finish_time }}</span>
						<span v-else>不限制时间</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="150px" align="center" fixed="right">
					<template scope="{$index, row}">
						<el-link class="btn-blue" :underline="false" @click="$refs.details.init(row)">
							详情
						</el-link>
						<el-link class="btn-blue" :underline="false" @click="openAnswer('编辑', row)">
							编辑
						</el-link>
						<el-link class="btn-blue" :underline="false" @click="onDelete(row, $index)">
							删除
						</el-link>
					</template>
				</el-table-column>
			</el-table>
			<el-row type="flex" justify="center">
				<el-col :span="24" style="text-align: right">
					<Pagination
						:total="total"
						:page.sync="pageNo"
						:limit.sync="pageSize"
						scroll-el="el-main"
						@pagination="pageChange"
					/>
				</el-col>
			</el-row>
		</div>
		<!-- 新增banner-->
		<Details ref="details" />
	</div>
</template>
<script>
	import { mapGetters } from 'vuex'
	import Details from './components/details.vue'
	import Pagination from '@/components/Pagination'
	import { getBarList, focusDelete } from '@/api/article.js'
	export default {
		name: 'lunbo',
		components: { Pagination, Details },
		data() {
			return {
				forms: {
					focus_title: '',
					date: []
				},
				pageNo: 1,
				pageSize: 10,
				total: 0,
				tableData: [] // table数据
			}
		},
		computed: {
			...mapGetters(['userid'])
		},
		activated() {
			this.loadData()
		},
		created() {
			this.loadData()
		},
		methods: {
			// 搜索
			searchData() {
				this.pageNo = 1
				this.loadData()
			},
			// 分页
			pageChange({ page, limit }) {
				this.pageNo = page
				this.pageSize = limit
				this.loadData()
			},
			async loadData() {
				const loading = this.$setLoading()
				getBarList({
					focus_title: this.forms.focus_title,
					start_time: this.forms.date.length ? this.forms.date[0] : '',
					end_time: this.forms.date.length ? this.forms.date[1] : '',
					page_no: this.pageNo,
					page_size: this.pageSize
				}).then(
					res => {
						this.tableData = res.data.lists
						this.total = Number(res.data.total)
						loading.close()
					},
					err => {
						loading.close()
					}
				)
			},
			openAnswer(type, row) {
				// this.$refs.addBanner.initData(type, row)
				this.$router.push({ path: '/lunbo/create', query: { id: row && row.focus_id } })
			},
			onDelete(row, index) {
				this.$confirm('确定要删除当前广告信息吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				})
					.then(async () => {
						const res = await focusDelete({
							focus_id: row.focus_id
						})
						this.$message.success('删除成功！')
						this.tableData.splice(index, 1)
					})
					.catch(() => {})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.data-desc {
		margin: 0 10px;
	}
	.copy {
		cursor: pointer;
	}
	.form {
		background: #ffffff;
		padding: 24px 10px 0;
		margin-bottom: 10px;
		border-radius: 2px;
		display: flex;
		flex-wrap: wrap;
	}

	.answer-bot {
		padding: 16px;
		border-radius: 2px;
		background-color: #ffffff;
		.bot-title {
			font-size: 16px;
			font-family: PingFangSC-Regular, PingFang SC;
			color: #000000;
		}
	}
	.btn-blue {
		color: #3674ff;
		font-size: 13px;
		cursor: pointer;
		margin: 0 5px;
	}
	.a_txt:hover {
		color: #3674ff;
		text-decoration: underline;
	}
	.line {
		height: 36px;
		margin: 0 0 10px 18px;
		border-left: 1px solid #e5e6eb !important;
		padding: 0 0 0 18px;
	}
	::v-deep {
		.pagination {
			text-align: right;
			padding: 0 16px 8px;
		}
	}
</style>
